import json

import pygal
from pygal.style import RotateStyle as RS,LightColorizedStyle as LCS

from country_codes import get_country_code

filename='population_data.json'
with open(filename) as f:
	pop_data=json.load(f)

# 包含人口的字典
cc_poplutaion={}

for pop_dict in pop_data:
	if pop_dict['Year']=='2010':
		country_name=pop_dict['Country Name']
		population=int(float(pop_dict['Value']))

		code=get_country_code(country_name)
		if code:
			cc_poplutaion[code]=population
		# 	print(code+': '+str(population))
		# else:
		# 	print('Error-'+country_name)

		# print(country_name+" "+str(population))

# 将人口分类，少于1000万，介于1000万到10亿间，大于10亿
cc_pops1,cc_pops2,cc_pops3={},{},{}
for cc,pop in cc_poplutaion.items():
	if pop<10000000:
		cc_pops1[cc]=pop
	elif pop <1000000000:
		cc_pops2[cc]=pop
	else:
		cc_pops3[cc]=pop

print(len(cc_pops1),len(cc_pops2),len(cc_pops3))

# 绘制地图
s=RS('#336699',base_style=LCS)
# s=RotateStyle('#ff0000')
worldmap_chart = pygal.maps.world.World(style=s)
worldmap_chart.title = 'World population in 2010, by Country'
# worldmap_chart.add('2010', cc_poplutaion)
worldmap_chart.add('0~10M', cc_pops1)
worldmap_chart.add('10~1BM', cc_pops2)
worldmap_chart.add('>1Bn', cc_pops3)

worldmap_chart.render_to_file('p.svg')